"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2022/5/9 17:45
@Software: PyCharm
@File    : ui_table_load_widget.py
@Remark  : 
"""
from PySide2.QtCore import Slot, Qt
from PySide2.QtWidgets import QWidget, QAbstractItemView, QTableWidget

from chart_core.data_qt_module.table_module import PauseTableWidget
from ui_component.ui_to_analysis_stdf.ui_designer.ui_table_load import Ui_Form as TableLoadForm
from ui_component.ui_to_analysis_stdf.utils import QTableUtils


class TableLoadWidget(QWidget, TableLoadForm):
    """
    Table
    .setFont(QFont(None, 8));
    .resizeRowsToContents()
    """
    parent = None

    def __init__(self, parent=None):
        super(TableLoadWidget, self).__init__(parent)
        self.parent = parent
        self.setupUi(self)
        self.setWindowTitle("Data TEST NO&ITEM Analysis")
        self.cpk_info_table = PauseTableWidget(self)  # type:QTableWidget
        self.cpk_info_table.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.cpk_info_table.setEditable(True)
        self.horizontalLayout.addWidget(self.cpk_info_table)

    @Slot(bool)
    def on_checkBox_clicked(self, e):
        if e:
            self.parent.table_load_widget.cpk_info_table.sortByColumn(11, Qt.SortOrder.DescendingOrder)
        else:
            self.parent.table_load_widget.cpk_info_table.sortByColumn(0, Qt.SortOrder.AscendingOrder)

    @Slot()
    def on_pushButton_pressed(self):
        new_limit = QTableUtils.get_all_new_limit(self.cpk_info_table)
        if not new_limit:
            return
        if self.parent.message_show("注意,Limit已经更新!!!,如果只看PASS请选择否!"):
            self.parent.li.new_limit(new_limit, False)
        else:
            self.parent.li.new_limit(new_limit, True)
        print("重算Yield, 重载print到TextBrowser上")

    @Slot()
    def on_pushButton_5_pressed(self):
        self.parent.li.restore_limit()

    @Slot()
    def on_pushButton_2_pressed(self):
        new_limit = QTableUtils.get_select_new_limit(self.cpk_info_table)
        if not new_limit:
            return
        self.parent.li.cut_data_by_limit("inner", new_limit)
        print("删除选中项目Limit外的数据")

    @Slot()
    def on_pushButton_4_pressed(self):
        text_column = QTableUtils.get_table_widget_test_text(self.cpk_info_table)
        if not text_column:
            return
        self.parent.li.screen_df(text_column)
        print("只对选中项目的数据进行分析")

    def cpk_table_row_hide(self, hide: bool):
        for i in range(self.cpk_info_table.rowCount()):
            self.cpk_info_table.setRowHidden(i, hide)

    @Slot()
    def on_lineEdit_returnPressed(self):
        """
        若可以查询到, 先隐藏所有行
        """
        regex = self.lineEdit.text()
        regex = "*{}*".format(regex)
        items = self.cpk_info_table.findItems(regex, Qt.MatchWildcard)
        if len(items) == 0:
            self.parent.mdi_space_message_emit("无法根据筛选条件查询到匹配行@!显示所有行.")
            self.cpk_table_row_hide(False)
            return
        self.cpk_table_row_hide(True)
        for each in items:
            if each.column() != 9:
                continue
            self.cpk_info_table.setRowHidden(each.row(), False)


if __name__ == '__main__':
    import sys
    from PySide2.QtWidgets import QMainWindow, QApplication

    app = QApplication(sys.argv)
    win = TableLoadWidget()
    win.show()
    sys.exit(app.exec_())
